package chihane.jdaddressselector;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

public class DefaultAddressProvider2 implements AddressProvider {

    private List<Province> addressData;
    List<City> cities = null;
    List<County> counties;
    public DefaultAddressProvider2(Context context) {
        addressData = AddressUtil.getData1(context);
    }

    public DefaultAddressProvider2(String addressInfo){
        addressData = JSONObject.parseArray(addressInfo, Province.class);
    }

    @Override
    public void provideProvinces(final AddressReceiver<Province> addressReceiver) {
        addressReceiver.send(addressData);
    }

    @Override
    public void provideCitiesWith(String provinceId, final AddressReceiver<City> addressReceiver) {

        for (int i = 0; i < addressData.size(); i++) {
            Province p = addressData.get(i);
            if (p.getValue().equals(provinceId)){
                cities = p.getCities();
            }
        }
        addressReceiver.send(cities);
    }

    @Override
    public void provideCountiesWith(String cityId, final AddressReceiver<County> addressReceiver) {

        for (int i = 0; i < cities.size(); i++) {
            City c = cities.get(i);
            if (c.getValue().equals(cityId)){
                counties = c.getAreaList();
            }
        }
        addressReceiver.send(counties);
    }

    @Override
    public void provideStreetsWith(String countyId, final AddressReceiver<Street> addressReceiver) {
        addressReceiver.send(new ArrayList<Street>());
    }
}
